local scene = director:createScene()

function scene:setUp(event)
    dbg.print("sceneGame:setUp")
	
	physics:setGravity(0, 10)
	
	local scaleX = 320/director.displayWidth
	local scaleY = 480/director.displayHeight
	local numberInitial = 7/scaleX
	math.randomseed (os.time ())
	local numberOfQuestions = 20
	local numberOfBalloons = 20
	balloon = {};
	line = {};
	numberBalloon = {};
	numberQuestion = {};
	textNumber = {};
	local isTouching = false;
	local gameRun = 1;
	local changeNumbers = 1;
	local positionNumber = 0;
	local numberActive = 0
	local changePosition = 1
	system.debugTime = nil
	system.gameTime = 0
	system.timers = {}
	local newTime = 0

	
	selectColor = 0;
	local timeText = director:createLabel({x=director.displayCenterX+director.displayCenterX/4, y=director.displayCenterY*7/4, text="Time: ", zOrder=2, hAlignment="left", xAnchor=0, color=color.white, xScale=1/scaleX, yScale=1/scaleY})
	
	local timeTextNumber = director:createLabel({x=timeText.x+70/scaleX, y=timeText.y, text="", zOrder=2, hAlignment="left", xAnchor=0, color=color.white, xScale=1/scaleX, yScale=1/scaleY})
	
	for i = 1, numberOfQuestions do
		textNumber[i] = director:createLabel({x=numberInitial+14/scaleX, y=50/scaleY, text="", zOrder=2, hAlignment="left", xAnchor=0, color=color.white, xScale=1/scaleX, yScale=1/scaleY})
		numberInitial = numberInitial+14/scaleX
	end
	
	local winLoseLabel = director:createLabel({x=director.displayCenterX, y=290/scaleY, text="", zOrder=2, hAlignment="center", xAnchor=0.5, color=color.green, isVisible=false, xScale=1/scaleX, yScale=1/scaleY})
	
	local winTimeLabelTwo = director:createLabel({x=director.displayCenterX, y=270/scaleY, text="", zOrder=2, hAlignment="center", xAnchor=0.5, color=color.green, isVisible=false, xScale=1/scaleX, yScale=1/scaleY})

	local winTimeLabel = director:createLabel({x=director.displayCenterX, y=250/scaleY, text="", zOrder=2, hAlignment="center", xAnchor=0.5, color=color.green, isVisible=false, xScale=1/scaleX, yScale=1/scaleY})
	
	local buttonRestart = director:createSprite(director.displayCenterX, 50/scaleY, "img/button.png")
	buttonRestart.xAnchor=0.5; buttonRestart.yAnchor=0.5; buttonRestart.xScale=.7/scaleX; buttonRestart.yScale=.5/scaleY; buttonRestart.zOrder=3;
	local labelRestart = director:createLabel({x=25, y=35, text="Restart", zOrder = 3, hAlignment="left", color=color.black,})
	buttonRestart:addChild(labelRestart); labelRestart.xScale=1.3; labelRestart.yScale=1.5
	buttonRestart.isVisible = false
			
	for i = 1, numberOfBalloons do
		balloon[i] = director:createSprite(-200/scaleX ,director.displayHeight+100/scaleY ,"img/balloons.png")
		balloon[i].xScale = (math.random(50, 70)/100)/scaleX
		balloon[i].yScale = (math.random(50, 70)/100)/scaleY
		if (math.random(1, 2) == 1) then
			line[i] = director:createSprite(balloon[i].h*4/10 ,5/scaleY,"img/lineOne.png")
		else
			line[i] = director:createSprite(balloon[i].h*4/10 ,5/scaleY,"img/lineTwo.png")
		end
		balloon[i]:addChild(line[i])
		line[i].zOrder = -1
		line[i].xAnchor=0.5; line[i].yAnchor=1
		balloon[i].isVisible = false
		balloon[i].y = math.random(100/scaleY, director.displayHeight+100/scaleY)
		balloon[i].xAnchor=0.5; balloon[i].yAnchor=0
		balloon[i].zOrder = 0
		physics:addNode(balloon[i], {radius = balloon[i].w/2})
		numberBalloon[i] = director:createLabel({x=balloon[i].h*3/10, y=balloon[i].w*3/10, hAlignment="left", color=color.black, xAnchor=0, yAnchor=0 })
		balloon[i]:addChild(numberBalloon[i])
		line[i].xScale = .7
		line[i].yScale = .7
		numberBalloon[i].xScale = .8+balloon[i].xScale
		numberBalloon[i].yScale = .8+balloon[i].yScale
	end
	
	function system:getTime()
		if self.debugTime then
			return self.debugTime
		else
			return self.gameTime
		end
	end

	local move = function()
	
		timeTextNumber.text = math.floor(system:getTime())
		
		if (changeNumbers == 1) then
			numberQuestion={}
			for i = 1, numberOfQuestions do 
				numberQuestion[i] = math.random(0, 9)
				textNumber[i].text = numberQuestion[i]
			end
			changeNumbers = 0
		end
		
		if (gameRun == 2) then
			winLoseLabel.text = "You win"
			winLoseLabel.isVisible = true
			winTimeLabelTwo.text ="Your time is"
			winTimeLabelTwo.isVisible = true
			winTimeLabel.isVisible = true
			for i = 1, numberOfBalloons do
				physics:removeNode (balloon[i])
				balloon[i].isVisible = false
				balloon[i].x = -200/scaleX
				physics:addNode(balloon[i], {radius = balloon[i].w/2})
			end
			buttonRestart.isVisible = true
			buttonRestart.x = director.displayCenterX
			buttonRestart.y = 50/scaleY
			timeTextNumber.isVisible = false
			timeText.isVisible = false
			
			local file = io.open("bestTimes.txt")
			leveltimesOne = tonumber(file:read())
			leveltimesTwo = tonumber(file:read())
			leveltimesThree = tonumber(file:read())
			leveltimesFour = tonumber(file:read())
			leveltimesFive = tonumber(file:read())
			file:close()
				
			if (newTime < leveltimesOne) then
				leveltimesFive = leveltimesFour
				leveltimesFour = leveltimesThree
				leveltimesThree = leveltimesTwo
				leveltimesTwo = leveltimesOne
				leveltimesOne = newTime
				newTime = 999
				elseif (newTime < leveltimesTwo) then
					leveltimesFive = leveltimesFour
					leveltimesFour = leveltimesThree
					leveltimesThree = leveltimesTwo
					leveltimesTwo = newTime
					newTime = 999
						elseif (newTime < leveltimesThree) then
						leveltimesFive = leveltimesFour
						leveltimesFour = leveltimesThree
						leveltimesThree = newTime
						newTime = 999
							elseif (newTime < leveltimesFour) then
							leveltimesFive = leveltimesFour
							leveltimesFour = newTime
							newTime = 999
								elseif (newTime < leveltimesFive) then
								leveltimesFive = newTime
								newTime = 999
			end
				
			file = io.open("bestTimes.txt", "w")
			file:write(leveltimesOne.."\n")
			file:write(leveltimesTwo.."\n")
			file:write(leveltimesThree.."\n")
			file:write(leveltimesFour.."\n")
			file:write(leveltimesFive.."\n")
			file:close()
			
		elseif (gameRun == 1) then
			newTime = math.floor(system:getTime())
			winTimeLabel.text = newTime
			for i = 1, numberOfBalloons do
				if (balloon[i].y >= director.displayHeight+50/scaleY) then
					winLoseLabel.isVisible = false
					winTimeLabelTwo.isVisible = false
					winTimeLabel.isVisible = false
					timeTextNumber.isVisible = true
					timeText.isVisible = true
					physics:removeNode (balloon[i])
					balloon[i].isVisible = true
					balloon[i].y = -100/scaleY
					balloon[i].x = math.random(50/scaleX, director.displayWidth-50/scaleX)
					selectColor = math.random(1, 5)
					if (selectColor == 1) then
						balloon[i].color = color.yellow
						numberBalloon[i].color = color.red
						elseif(selectColor == 2) then
							balloon[i].color = color.blue
							numberBalloon[i].color = color.white
							elseif(selectColor == 3) then
								balloon[i].color = color.green
								numberBalloon[i].color = color.white
							elseif(selectColor == 4) then
								balloon[i].color = color.fuchsia
								numberBalloon[i].color = color.white
						else
						balloon[i].color = color.aqua
						numberBalloon[i].color = color.red
					end
					if (i <= 10) then
						numberBalloon[i].text = i-1
					else
						numberBalloon[i].text = i-11
					end
					balloon[i].name = numberBalloon[i].text
					physics:addNode(balloon[i], {radius = balloon[i].w/2})
				end
			end
		else
			winLoseLabel.text = "You lose"
			winLoseLabel.isVisible = true
			for i = 1, numberOfBalloons do
				physics:removeNode (balloon[i])
				balloon[i].isVisible = false
				balloon[i].x = -200/scaleX
				physics:addNode(balloon[i], {radius = balloon[i].w/2})
			end
			buttonRestart.isVisible = true
			buttonRestart.x = director.displayCenterX
			buttonRestart.y = 50/scaleY
			timeTextNumber.isVisible = false
			timeText.isVisible = false
		end
		
		function buttonRestart:touch(event)
				if (event.phase == 'began') then
						winLoseLabel.isVisible = false
						winTimeLabelTwo.isVisible = false
						winTimeLabel.isVisible = false
						numberQuestion = {};
						gameRun = 1;
						changeNumbers = 1;
						positionNumber = 0;
						numberActive = 0
						changePosition = 1
						system.debugTime = nil
						system.gameTime = 0
						system.timers = {}
						buttonRestart.isVisible = false
						buttonRestart.x = -500
						buttonRestart.y = -500
				end
			end
			buttonRestart:addEventListener('touch', buttonRestart)
			
		if (changePosition == 1) then
			positionNumber = positionNumber+1
			for i = 1, numberOfQuestions do
				if (positionNumber == i) then
					textNumber[i].color = color.red
					numberActive = textNumber[i].text
				else
					textNumber[i].color = color.white
				end
			end
			changePosition = 0
		end
	end
	system:addEventListener("update", move)
	
	local onTouch = function(event)
		local balloonTouch = event.target
		if (event.phase == "began") then 
			if (balloonTouch ~= nil) then
				audio:playStream("sound/pop.mp3", false)
				physics:removeNode (balloonTouch)
				balloonTouch.y = director.displayHeight+150/scaleY
				for i = 1, numberOfQuestions do
					if ( balloonTouch.name == numberActive) then
						if (positionNumber == numberOfQuestions) then
							gameRun = 2
						else
							changePosition = 1
						end
					else
						gameRun = 0
					end
				end
				physics:addNode(balloonTouch, {radius=balloonTouch.w/2})
			end
		end
	end
	for i = 1, numberOfBalloons do
	balloon[i]:addEventListener("touch", onTouch)
	end
    system:addEventListener("touch", onTouch)
	

end

function scene:tearDown(event)
    dbg.print("sceneGame:tearDown")
end

scene:addEventListener({"setUp", "tearDown"}, scene)
return scene -- We must return the scene object!